package cn.iniche.utils;

import android.os.Build;
import android.text.TextUtils;

import java.util.Locale;

public final class StringUtils {

    public static final String USER_AGENT = getDefaultUserAgent();

    public static String toDBC(final String s) {
        if (s == null || s.length() == 0) return "";
        char[] chars = s.toCharArray();
        for (int i = 0, len = chars.length; i < len; i++) {
            if (chars[i] == 12288) {
                chars[i] = ' ';
            } else if (65281 <= chars[i] && chars[i] <= 65374) {
                chars[i] = (char) (chars[i] - 65248);
            } else {
                chars[i] = chars[i];
            }
        }
        return new String(chars);
    }

    private static String getDefaultUserAgent() {

        StringBuilder builder = new StringBuilder("Mozilla/5.0 (Linux; Android ");

        //add android version, like 5.1, 7.1, 7.1.1
        String release = Build.VERSION.RELEASE;
        if (TextUtils.isEmpty(release)) release = "1.0";
        builder.append(release).append("; ");

        //add system language, like zh-CN
        Locale locale = Locale.getDefault();
        String language = locale.getLanguage();
        if (TextUtils.isEmpty(language)) {
            language = "zh";
            builder.append(language);
        } else {
            String country = locale.getCountry();
            builder.append(language);
            if (!TextUtils.isEmpty(country)) {
                builder.append('-').append(country);
            }
        }
        builder.append("; ");

        //add device info
        String model = Build.MODEL;
        if (TextUtils.isEmpty(model)) model = "UNKNOWN_DEVICE";
        builder.append(model).append(' ');

        String id = Build.ID;
        if (!TextUtils.isEmpty(id))
            builder.append("Build/").append(id);
        builder.append(") ");

        //add app version
        builder.append("JL-Iniche");

        return builder.toString();
    }
}
